<?php

 class Connection {
    
    private $_host;
    private $_username;
    private $_password;
    private $_dbname;
    private $_connection_state;
    
    function __construct(){
        
        $this->_host = "localhost";
        $this->_username = "root";
        $this->_password = "";
        $this->_dbname = "dttest";
        
        
    }
    
    private function _initialize_connection(){
        
        $this->_connection_state = new mysqli($this->_host,$this->_username,$this->_password,$this->_dbname);
        return $this->_connection_state;
        
    }
    
   public function query($sql = ""){
    $connection = $this->_initialize_connection();
    
    //check for connection
    if($connection->connect_errno){
        
        return "Connection has failed. Error number :".$connection->connect_errno;
    }
    
    // execute the query
    if($result = $connection->query($sql)){
        if($result->num_rows > 0){
            while($row = $result->fetch_array()){
                $list[] = $row;
                
            }
            
            
        }
        
    }
    
    //dispose
    //$result->close();
    $connection->close();
    
    
    //return value;
    return $list;
    
   }
   
   public function transact($sql = ""){
    
    $connection = $this->_initialize_connection();
    
    //Check for connection
    if($connection->connect_errno){
        return "Connection has failed.Error number : ".$connection->connect_errno;
    }
    
    //execute
    if(!$connection->query($sql)){
        $connection->close();
        $res = "Failed";
        return $res;
    }
    $connection->close();
    return true;
    
    
    //
   }
 }

?>